

rm(list=ls())#clear Global Environment
setwd('./Fig.3A_transcripts_volcano')


library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(dplyr) # A Grammar of Data Manipulation
library(ggfittext) # Fit Text Inside a Box in 'ggplot2'
library(ggrepel) # Automatically Position Non-Overlapping Text Labels with 'ggplot2'
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization



df<-read.table(file="data_transcripts.txt",sep="\t",header=T )

df <- df %>%
  filter(!is.na(df[[3]]) & !is.na(df[[4]]))

df%>%
  dplyr::mutate(group=group%>%
                  factor(levels=c("DR_R", "R_OR", "OR_OY",
                                  "OY_Y", "Y_DR")))%>%
  dplyr::mutate(group2=ifelse(p_value<0.05&abs(log2FC)>=2,
                              ifelse(log2FC>=2,"up","down"),"NS")%>%
                  factor(levels=c("up","down","NS")))%>%
  dplyr::mutate(label=ifelse(p_value<0.05&abs(log2FC)>=4,"Y","N"))%>%
  dplyr::mutate(label=ifelse(label=="Y",as.character(Name),''))%>%
  dplyr::mutate(G=ifelse(log2FC>=0,"Sigdown","Sigup"))%>%
  dplyr::select(Name,group,log2FC,p_value,group2,label,G)->df
df%>%
  dplyr::group_by(group)%>%
  dplyr::summarise(max_log2FC=max(log2FC),
                   min_log2FC=min(log2FC))%>%
  as.data.frame->df_bg


# View(df_bg)
# # Draw gray background columns in each group of data points in the diagram.
p <- ggplot()+
  # # Gray background of positive half axis of Y axis
  geom_col(data = df_bg, 
           mapping = aes(group,max_log2FC),
           fill = "grey85", width = 0.8, alpha = 0.5) +
  ##Gray background of negative semi-axis of Y axis
  geom_col(data = df_bg, 
           mapping = aes(group, min_log2FC),
           fill = "grey85", width = 0.8, alpha = 0.5)


##Add data points of each group
p1 <- p+geom_jitter(data = df,
                    mapping = aes(x = group, y = log2FC, color = group2),
                    size= 3,width =0.4, alpha = 0.7)
p1

##Show the grouping information by adding a box at the position of X=0.
#There are two ways to achieve this. 1) The column with gray background is added in the same way, using the geom_col function.
p2 <- p1+geom_col(data = df_bg,
                  mapping = aes(x= group, y = 0.3, fill = group))+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -0.3, fill = group))
p2


##Add grouped text messages in the box.
p3 <- p2+geom_text(data=df_bg,
                   mapping = aes(x=group, y=0, label=group),
                   size = 4, color ="#dbebfa")
p3



p8<-ggplot()+
 
  geom_col(data = df_bg, 
           mapping = aes(group,max_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5) +
  
  geom_col(data = df_bg, 
           mapping = aes(group, min_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5)+
  # Add data points of each group and color them according to whether log2FC is greater than 0.
  geom_jitter(data = df,
              mapping = aes(x = group, y = log2FC, color = G),
              size= 3,width = 0.4, alpha = 0.9)+
  # The grouping information is displayed by adding a box at the position of X=0, and it is added by the geom_col method.
  geom_col(data = df_bg,
           mapping = aes(x= group, y = 0.6, fill = group),
           width = 0.8, show.legend = F)+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -0.6, fill = group),
           width = 0.8, show.legend = F)+
  # Add grouped text information in the box.
  geom_fit_text(data=df_bg,
                mapping = aes(x=group, y=0, label=group),
                size = 20, color ="black")+
  # Convert to polar coordinates
  coord_polar(direction=1)+
 # Add a salience tag
  geom_text_repel(data = df,
                  mapping = aes(x = group, y = log2FC, label = label),
                  max.overlaps = 10000,
                  size=3,
                  box.padding=unit(0.3,'lines'),
                  point.padding=unit(0.3, 'lines'),
                  segment.color='black',
                  show.legend=FALSE)+

  theme_void()+

  scale_fill_manual(values = c("DR_R"="#0ebeff", "R_OR"="#47cf73", "OR_OY"="#ae63e4",
                               "OY_Y"="#fcd000", "Y_DR"="#ff3c41"))+
  scale_color_manual(values = c("#a4c639","#b52e31"),name=NULL)+
  guides(
    color = guide_legend(
      override.aes = list(size = 7, shape = 20)))

p8


p9<-ggplot()+
 
  geom_col(data = df_bg, 
           mapping = aes(group,max_log2FC),
           fill = "grey95", width = 0.8, alpha = 0.8) +
  
  geom_col(data = df_bg, 
           mapping = aes(group, min_log2FC),
           fill = "grey95", width = 0.8, alpha = 0.8)+
  
  geom_jitter(data = df,
              mapping = aes(x = group, y = log2FC, color = group2),
              size= 1,width = 0.4, alpha = 0.5)+
  
  geom_col(data = df_bg,
           mapping = aes(x= group, y = 1.4, fill = group),
           width = 0.8, show.legend = F)+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -1.4, fill = group),
           width = 0.8, show.legend = F)+


  coord_polar(direction=1)+

  
  theme_void()+
  scale_fill_manual(values = c("DR_R"="#0ebeff", "R_OR"="#47cf73", "OR_OY"="#ae63e4",
                               "OY_Y"="#fcd000", "Y_DR"="#ff3c41"))+
  scale_color_manual(values = c("#e42313", "#0061d5", "#8b8c8d"),name=NULL)+
  guides(
    color = guide_legend(
      override.aes = list(size = 7, shape = 20))) + # 在方块中添加分组的文字信息
geom_fit_text(data=df_bg,
              mapping = aes(x=group, y=0, label=group),
              size = 40, color ="black")
p9
ggsave("p8.svg",p8)
ggsave("p9.svg",p9)
##Finally, export the picture to PDF format and adjust some details in AI or PS.

